<?xml version="1.0" encoding="UTF-8"?>
<html>
    <head>
        <link type="text/css" rel="stylesheet" href="./css/template.css" />
        <link type="text/css" rel="stylesheet" href="./css/SyntaxHighlighter.css" />
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Presentation of JavaSound - Article 1/1 - Java &amp; Solaris by the SUPINFO Sun technologies Laboratory   </title>
        <script language="javascript" src="./js/shInit.js" />
        <script language="javascript" src="./js/shCore.js" />
        <script language="javascript" src="./js/shBrushCpp.js" />
        <script language="javascript" src="./js/shBrushCSharp.js" />
        <script language="javascript" src="./js/shBrushCss.js" />
        <script language="javascript" src="./js/shBrushDelphi.js" />
        <script language="javascript" src="./js/shBrushJava.js" />
        <script language="javascript" src="./js/shBrushJScript.js" />
        <script language="javascript" src="./js/shBrushPhp.js" />
        <script language="javascript" src="./js/shBrushPython.js" />
        <script language="javascript" src="./js/shBrushRuby.js" />
        <script language="javascript" src="./js/shBrushSql.js" />
        <script language="javascript" src="./js/shBrushVb.js" />
        <script language="javascript" src="./js/shBrushXml.js" />
        <keywords>javasound,sound,midi,wave,Java,Java SE,Sound,presentation,of,javasound,articles,4,4,certification,sun,java,solaris,articles,essentials,tips,tricks,codes,example,tutorial,development,jsp,servlet,jdbc,jdo,j2se,j2ee,laboratory</keywords>
        <author>Vanessa Colognac</author>
    </head>
    <body>
        <div class="chapter">
            <h2>Presentation of JavaSound</h2>
            <p>JavaSound is a collection of classes specified at the musical and sonorous programming. Since the version   1.3 of Java2, JavaSound forms a part of the library of classes under the form of packages javax.sound.midi and javax.sound.sampled.</p>
            <ul>
                <li>Javax.sound.midi : Class to play, record and synthesize the audio files at the MIDI format.</li>
                <li>Javax.sound.sampled : Class to play, record and mix recorded audio files.</li>
            </ul>
            <p>The JavaSound library supports all the audio formats are able to be read in the applets and the applications, namely AIFF, AU, WAV and the MIDI formats (type0, type1 and RMF)</p>
        </div>
        <div class="chapter">
            <h2>The MIDI files</h2>
            <p>The java.sound.midi  package bears musical MIDI files. The MIDI format (Musical Instrument Digital Interface) permits to stock the sounds as series of notes and produced effects by synthesized instruments by computer.</p>
            <p>In the MIDI files, we stock instructions noticing at MIDI sequencers how the sounds must be duplicated, what synthesized instructions to use, and other aspects of the presentation. The sound of a MIDI file depends of the quality and the variety of the available instructions on the computer or reading device.</p>
            <p>Each element of a MIDI presentation is represented by an interface or a class of javax.sound.midi package. They are the Sequencer interface and Sequence, Track and MidiEvent classes. The MidiSystem class permits to access at resources of reading and of storage MIDI on a computer system.</p>
        </div>
        <div class="chapter">
            <h2>MIDI file reading</h2>
            <p>To read a MIDI file with the help of JavaSound, you must create a Sequencer object. </p>
            <p>The getSequencer()  method of the MidiSystem class returns an Sequencer object which represents the sequencer by default of a system :</p>
            <textarea name="code" class="java">MIDI Sequencer = MidiSystem.getSequencer() ;</textarea>
            <p>This method of class generates an exception (MidiUnavaibleException) if the sequencer is not available for a reason or another.</p>
            <p>After creating a Sequencer object can read the MIDI files, you must call another method of the MidiSystem class to access at a MIDI sequence à from a source of data. These methods are:</p>
            <ul>
                <li>getSequence(File) : Load a sequence from the specified file.</li>
                <li>getSequence(URL) : Load a sequence from the specified Internet adress.</li>
                <li>getSequence(InputStream) : Load a sequence from a flux of entries of data, which can come from a file, from an input device or from another program.</li>
            </ul>
            <p>To load a MIDI sequence from a file, we must create an object File in first, in using its file name or a reference at this file name and at folder wherein finds itself.</p>
            <p>If the file finds itself in the same folder that the Java program, we can create it with the help of the File(String)  constructor method of this name.</p>
            <p>Below, here the instruction which permits to create a File object for a MIDI file called wzer.mid:</p>
            <textarea name="code" class="java">File sound = new File(« wzer.mid ») ;
				//If the file finds itself in a folder on one side:
				File sound = new File(« chemin/wzer.mid ») ;</textarea>
            <p>The File constructor generates an exception : NullpointerException if the argument of the constructor method possesses  a null value.</p>
            <p>Once the File object has been created, we must call the getSequence(File) method to create a sequence:</p>
            <textarea name="code" class="java">File sound = new File(« wzer.mid ») ;
				Sequence seq = MidiSystem.getSequence(sound);</textarea>
            <p>If everything happens well, the method of the class getSequence() will return a Sequence object.</p>
            <p>Else, two  kinds of errors can be generated by the method :</p>
            <ul>
                <li>InvalidMidiDataException if the system cannot generate the MIDI data.</li>
                <li>IoException If the input of the file has been interrupted or has failed for whatever reason.</li>
            </ul>
            <p>So we dispose of a MIDI sequencer and of a sequence to read. To read a sequence :</p>
            <ul>
                <li>Call the method open() of sequencer in order that the platform gets ready to read a file.</li>
                <li>Call the method start() of sequencer to start to read the sequence.</li>
                <li>Wait the end of the playing of the sequence.</li>
                <li>Wait the method close() of sequencer to return the available appliance at other tasks.</li>
            </ul>
        </div>
        <div class="chapter">
            <h2>Manipulation of audio files</h2>
            <p>The advantages of JavaSound become manifest at the moment of manipulation of the audio files. Indeed, we can modify the aspect of the presentation and of the recording of audio files with the help of JavaSound packages.</p>
            <p>To execute this operation on an existing Sequencer object, you must call its setTempoFactor(float) method. The tempo is represented by a float value starting 0.0.</p>
            <p>To accelerate the current tempo, you must call the getTempoFactor() method, which returns a float value.</p>
            <p>Here, this article is now finish. I hope that it, you will permit to play with the audio and MIDI files...</p>
        </div>
    </body>
</html>

